package main

import (
	"crypto/md5"
	"encoding/base64"
	"fmt"
	"io/ioutil"
	"strings"

	"golang.org/x/text/encoding/charmap"
	"golang.org/x/text/transform"
)

func main() {

	salt := "ADAKKAMAMARARULAARIRULUYITHUVIDUM"
	//authtoken := "176BADFC-AF25-4360-AB0C-1565D3EA3264" //
	authtoken := "5494CB32-F783-4DEB-AAE1-F326026F2679"
	//authtoken := "FC8CD6EF-464C-40ED-9FA9-D691B778C0CD"
	m5 := md5.New()
	latin1, _ := charmap.ISO8859_1.NewDecoder().Bytes([]byte(authtoken))
	salt1 := transform.NewReader(strings.NewReader(salt), charmap.ISO8859_1.NewDecoder())
	salt_bytes, _ := ioutil.ReadAll(salt1)
	m5.Write(latin1)
	m5.Write(salt_bytes)
	st := m5.Sum(nil)
	fmt.Println("base 64 en------ciode ", base64.StdEncoding.EncodeToString(st))
}

func getJosn() {

}


func encryptPassword() {
	token := uuid.NewV4()
	// token := "af09eb2b-b970-4587-8d2b-b9ed42d54c50"
	// eHPT4DXBpSr5N08DNKKsWDrrXT8bxvh62oiZDzqwI8bo3rlmNjcwIL5lYZjPFhZ7oggDXSFtS9cs7a09F5yYIcDBhKPtcOgzNBkHRdchzbXrbpDlMP5ocnPL+ysxxQAA7LbZslhoPRqZ9JrI+AhwB16WmMVKK/GkDbQovUlPEjD80tA19FxKNHSaHR0k1nCpXTXLKnaYM/JT4K+2a7bchLuM8/bCwNo0mIT+2eSUsOKODS09cDEslDdU7/n9YjM3iudKEzElkE/WLO4FUPQxwktqaqEtX6n6SNscITz9tuVlWE9Ei3dvtzAYeAhZl8bN5cF02pJzVr2KECc3Qy4yzg==
	// X8aozSz7BD+GIash5pgnlAiE4v3FnMX+aPE1uRxW4JSMNdWy0EDozBjS1Ul+EMhbyLH0Q6XUHXcugjx1xp6gyywPX9I+PhA2GCqRF1XiF+RrdYySO6sVc2frUQvbEJemosDEbIFrtrsviKKv4eARLrWsSljcrawx+Yk/c2/rnvOWPFCqVm2dbG3Af1xxTqJRDKUEyYwwpSLOmylJbes22sW1pNVa6L6VywUZumv44FbSh6AEDPu56ivsZKq2x1T7zNDsxVHpvtPtZ4GqC+HBfQmMJ1D7fknru7Uv/+AUi/e3pxF7Nx79gh5adeD0WeRqpHZF3CevB2IcL1MunkFvgA==
	// TIGGAxq6E53zOv/nRvyaE7iZ/uBcQCPB15iPomNiROuFauAbONjZrSmJhEqkdSPvo5KNOLscJes1r6KKgDezU2Z/RCtztblWglaxsceC32LHFd5hU3YDiMcYP0MXRuEyZoqwiQevuMa0ukYT9KoeTywKJNgOJDx2Jd94B56niIMzzxpksmWRteN3ff6POj14vWeH1CU9QfZ1eqmW4Vqq29Q3dKLwEU3PnZZyc+N4xeN8eOPWJsng+ZrY7qdqpM5sOu21dhMw4fvPPXsuek8c4/bN/zIDIJro/Tu5ml8qIQTO04E2s9lL+VcDvJiPEBcCfHJNu0VrIgv9GUPxYLQrwQ==
	//pub_key := `MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAh44SJsFGcr1bUXXAH8wL11+Ga8outl7fTERfVvdXIO1H4l6neJU3/Mw9yEiaeo+Fl0EwTepfRHPDy1iYygsYtAvOtwb4Ohcu8yznzyWGwdxIxjoQp6r9zQVHwU3Wu9y4HiLMhEtLlDGyNvaeuYY6fYGKIx3uyEFKmDsx+I0qNig5ZVhOkdewrJTvEXnCQAyAtpIgOUGkQnTqbctfKnXblM8L2AgxfW+9G0X61rFQ/yTwS1O8RIbHLzTP1xfh8l8/XfosPffo1f2djAA30luICM3CO5xyi3+g0McxtYxlvEEPAJpsAq8Omdt4Uw0ResdzfGw9NGXGI65XkYSkaoi80wIDAQAB`
	plainPassword := "Ad6<B[4upvZ!" + "---###---" + token.String()
	//
	fmt.Println("plainPassword  ", token.String())
	pub_pem := `
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAh44SJsFGcr1bUXXAH8wL
11+Ga8outl7fTERfVvdXIO1H4l6neJU3/Mw9yEiaeo+Fl0EwTepfRHPDy1iYygsY
tAvOtwb4Ohcu8yznzyWGwdxIxjoQp6r9zQVHwU3Wu9y4HiLMhEtLlDGyNvaeuYY6
fYGKIx3uyEFKmDsx+I0qNig5ZVhOkdewrJTvEXnCQAyAtpIgOUGkQnTqbctfKnXb
lM8L2AgxfW+9G0X61rFQ/yTwS1O8RIbHLzTP1xfh8l8/XfosPffo1f2djAA30luI
CM3CO5xyi3+g0McxtYxlvEEPAJpsAq8Omdt4Uw0ResdzfGw9NGXGI65XkYSkaoi8
0wIDAQAB
-----END PUBLIC KEY-----`
	//pub_pem = `-----BEGIN PUBLIC KEY-----MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAh44SJsFGcr1bUXXAH8wL11+Ga8outl7fTERfVvdXIO1H4l6neJU3/Mw9yEiaeo+Fl0EwTepfRHPDy1iYygsYtAvOtwb4Ohcu8yznzyWGwdxIxjoQp6r9zQVHwU3Wu9y4HiLMhEtLlDGyNvaeuYY6fYGKIx3uyEFKmDsx+I0qNig5ZVhOkdewrJTvEXnCQAyAtpIgOUGkQnTqbctfKnXblM8L2AgxfW+9G0X61rFQ/yTwS1O8RIbHLzTP1xfh8l8/XfosPffo1f2djAA30luICM3CO5xyi3+g0McxtYxlvEEPAJpsAq8Omdt4Uw0ResdzfGw9NGXGI65XkYSkaoi80wIDAQAB-----END PUBLIC KEY-----`
	block, _ := pem.Decode([]byte(pub_pem))
	if block == nil {
		panic("failed to parse PEM block containing the public key")
	}
	// privateKey, err := rsa.GenerateKey(bytes.NewReader([]byte(pub_key)), 2048)
	pubKey, err := x509.ParsePKIXPublicKey(block.Bytes)
	if err != nil {
		fmt.Println(" privateKey err:  ", pubKey, err)
		panic(err)
	}
	//public_Key := *rsa.PublicKey{PublicKey}
	public_Key := pubKey.(*rsa.PublicKey)
	//fmt.Println("public_Key ", public_Key )

	// public_Key := cert_body.PublicKey.(*rsa.PublicKey)
	encryptedBytes, err := rsa.EncryptPKCS1v15(rand.Reader, public_Key, []byte(plainPassword))
	if err != nil {
		fmt.Println(" encryptedBytes err:  ", public_Key)
		panic(err)
	}
	post_data := url.Values{}
	post_data.Add("AUTHRULE_NAME", "RememberMeLoginModule")
	post_data.Add("sdplogincsrfparam", token.String())
	post_data.Add("j_username", "administrator")
	post_data.Add("j_password", base64.StdEncoding.EncodeToString(encryptedBytes))
	post_data.Add("dname", "0")
	post_data.Add("DOMAIN_NAME", "-")
	post_data.Add("LDAPEnable", "false")
	post_data.Add("AdEnable", "false")
	post_data.Add("enableDomainDropdown", "true")
	post_data.Add("DomainCount", "2")
	post_data.Add("LocalAuth", "No")
	post_data.Add("LocalAuthWithDomain", "Not in Domain")
	post_data.Add("dynamicUserAddition_status", "true")
	post_data.Add("localAuthEnable", "true")
	post_data.Add("logonDomainName", "-1")
	post_data.Add("loginButton", "")
	req, _ := http.NewRequest("POST", "http://localhost:8542/j_security_check", strings.NewReader(post_data.Encode()))
	// auth_token := user.GetUserToken(user_name)
	req.Header.Add("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
	res, err := http.DefaultClient.Do(req)
	if err != nil {
		fmt.Println("requset error :", err)
		// return Response, err
	}
	defer res.Body.Close()
	//body, _ := ioutil.ReadAll(res.Body)
	fmt.Println("plainPassword-------------  ", token.String())
	fmt.Println("encrypted bytes: ", res, token, base64.StdEncoding.EncodeToString(encryptedBytes))
}